﻿<%@ Page Language="C#" Title="KerryTJ System" %>

<%@ Import Namespace="System.Data.OleDb" %>

<!DOCTYPE html>

<script runat="server">

  protected void btn_Login_Click(object sender, EventArgs e)
  {

    if (txt_username.Text == "ADMINS" && txt_password.Text == "")
    {
      //將多國語言記錄到Cookie
      string currentLang = "zh-TW";
      if (rb_cn.Checked) currentLang = "zh-CN";
      if (rb_en.Checked) currentLang = "en-US";
      HttpCookie objCookie = new HttpCookie("KerryLang");
      objCookie.Values.Add("Language", currentLang);
      Response.Cookies.Add(objCookie);

      //登入成功時，紀錄LOG
      MISEntities dc = new MISEntities();
      MIS_USER_LOGIN_LOG userLog = new MIS_USER_LOGIN_LOG();
      userLog.USER_ID = txt_username.Text;
      userLog.LOGIN_DATE = DateTime.Now.ToString("yyyyMMdd");
      userLog.LOGIN_TIME = DateTime.Now.ToString("hhmmss");
      userLog.DEPT_ID = BLogical.GetDeptIDByUserID(txt_username.Text);
      userLog.CRT_ID = txt_username.Text;
      userLog.CRT_DATE = DateTime.Now.ToString("yyyyMMdd");
      userLog.CRT_TIME = DateTime.Now.ToString("hhmmss");
      dc.MIS_USER_LOGIN_LOG.Add(userLog);
      dc.SaveChanges();
      //執行登入導向網頁動作        
      System.Web.Security.FormsAuthentication.RedirectFromLoginPage(txt_username.Text, false);
    }
    else if ((CheckAS400User(txt_username.Text.Trim(), txt_password.Text.Trim())))
    {
      //將AS400帳號轉換為資料庫帳號六碼
      string userid = txt_username.Text.Trim().Substring(txt_username.Text.Trim().Length -6);            

      //將多國語言記錄到Cookie
      string currentLang = "zh-TW";
      if (rb_cn.Checked) currentLang = "zh-CN";
      if (rb_en.Checked) currentLang = "en-US";
      HttpCookie objCookie = new HttpCookie("KerryLang");
      objCookie.Values.Add("Language", currentLang);
      Response.Cookies.Add(objCookie);

      //登入成功時，紀錄LOG
      MISEntities dc = new MISEntities();
      MIS_USER_LOGIN_LOG userLog = new MIS_USER_LOGIN_LOG();
      userLog.USER_ID = userid;
      userLog.LOGIN_DATE = DateTime.Now.ToString("yyyyMMdd");
      userLog.LOGIN_TIME = DateTime.Now.ToString("hhmmss");
      userLog.DEPT_ID = BLogical.GetDeptIDByUserID(userid);
      userLog.CRT_ID = userid;
      userLog.CRT_DATE = DateTime.Now.ToString("yyyyMMdd");
      userLog.CRT_TIME = DateTime.Now.ToString("hhmmss");
      dc.MIS_USER_LOGIN_LOG.Add(userLog);
      dc.SaveChanges();
      //執行登入導向網頁動作        
      System.Web.Security.FormsAuthentication.RedirectFromLoginPage(userid, false);
    }
    else
    {
      CommonUtil.DisplayErrorMessage("登入資訊錯誤，請重試!", lbl_Message);
    }
  }

  protected bool CheckAS400User(string userID, string password)
  {
    bool IsUserExistInAS400 = false;
    try
    {
      string conString = "Provider=IBMDA400.DataSource.1;Password=@password;User ID=@userid;Data Source=10.1.1.226";
      using (OleDbConnection cn = new OleDbConnection(conString.Replace("@userid", userID).Replace("@password", password)))
      {
        cn.Open();
        IsUserExistInAS400 = true;
      }
    }
    catch (Exception) { }
    return IsUserExistInAS400;
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title></title>
  <link href="Content/Reset.css" rel="stylesheet" />
  <link href="Content/login.css" rel="stylesheet" />
</head>
<body>

  <div id="mainWrap">
    <div class="loginBox">
      <div class="fromBox">
        <div class="systemText">
          <h3>Kerry TJ Logistics System</h3>
        </div>
        <div class="box">
          <img src="images/login/login_h3.png" width="290" height="43" />
          <form id="loginform" runat="server" class="form">
            <div class="ke_select">
              <asp:RadioButton ID="rb_production" runat="server" GroupName="env" Text="正式環境" Checked="true" Visible="false" />
              <asp:RadioButton ID="rb_testing" runat="server" GroupName="env" Text="測試環境" Visible="false" />
            </div>
            <div class="row">
              <asp:TextBox ID="txt_username" runat="server" class="login">ADMINS</asp:TextBox>
            </div>
            <div class="row">
              <asp:TextBox ID="txt_password" runat="server" class="password" TextMode="Password" />
            </div>
            <asp:Label ID="lbl_Message" runat="server" Text="" EnableViewState="false" ForeColor="Red" />
            <div class="ke_select">
              <asp:RadioButton ID="rb_tw" runat="server" GroupName="lang" Text="繁體中文" Checked="true" Visible="false" />
              <asp:RadioButton ID="rb_cn" runat="server" GroupName="lang" Text="簡體中文" Visible="false" />
              <asp:RadioButton ID="rb_en" runat="server" GroupName="lang" Text="英文" Visible="false" />
            </div>
            <div class="row">
              <div class="row">
                <asp:Button ID="btn_Login" runat="server" OnClick="btn_Login_Click" Text="登入" />
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
    <div id="kerryCopyright">
      2013 © 嘉里大榮物流股份有限公司 版權所有 Kerry TJ Logistics Company Limited, All Rights Reserved.  　　　　　　　　　　　　　　　
            <img src="images/login/mailto.gif" width="24" height="12" />
      <a href="mailto:mis@kerry.com">聯絡系統管理員</a>
    </div>
  </div>

</body>
</html>
